package com.guigu.qingcheng.servicebusiness.controller;


import com.guigu.qingcheng.pojo.bussiness.TbAd;
import com.guigu.qingcheng.servicebusiness.service.TbAdService;
import com.guigu.qingcheng.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/servicebusiness/tb-ad")
public class TbAdController {

    @Resource
    private TbAdService tbAdService;

    @GetMapping("/findAll")
    public R findAll(){
        List<TbAd> list = tbAdService.list(null);
        return R.ok().data("list",list);
    }

    @PostMapping("/findPage")
    public R findPage(@RequestBody Map<String,Object> searchMap, Integer page, Integer size){
        R page1 = tbAdService.findPage(searchMap, page, size);
        return R.ok().data("page1",page1);
    }

    @GetMapping("/findById")
    public R findById(@RequestParam("id") Integer id){
        TbAd byId = tbAdService.findById(id);
        return R.ok().data("byId",byId);
    }

    @PostMapping("/insert")
    public R insert(@RequestBody TbAd tbAd){
        boolean save = tbAdService.save(tbAd);
        if (save)
        return R.ok();
        return R.error();
    }

    @PostMapping("/update")
    public R update(@RequestBody TbAd tbAd){
        boolean b = tbAdService.updateById(tbAd);
        if (b) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @GetMapping("/delete")
    public R delete(@Param("id") Integer id){
        boolean b = tbAdService.removeById(id);
        if (b)
        return R.ok();
        return R.error();
    }

    // 根据位置查询广告列表
    @GetMapping("/findByPosition")
    public List<TbAd> findByPosition(String position) {
        return tbAdService.findByPosition(position);
    }

}

